// pages/yq/detail.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    fontSize: 16,
    id: 0,
    detail: {},
    user: {},
    detail_content: {},
    loaddinghidden: true,
    showLoading: false,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var now = new Date().getTime() / 1000;
    this.setData({
      nowTime: now
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 页面初始化 options 为页面跳转所带来的参数
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function(res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    var user = wx.getStorageSync("user");

    if (user) {
      that.setData({
        user: user,
      });
    }

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },
  loadData: function() {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;
    //获取数据
    http.ApiGet(apiUrl.UrlYqDetail, parameter, function(res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var detail_content = res.data.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
          .replace(/<section/g, '<div')
          .replace(/\/section>/g, '\div>');

        that.setData({
          loaddinghidden: true,
          detail: res.data,
          detail_content: detail_content,
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadData();
  },
  copyTap: function(e) {
    var val = e.currentTarget.dataset.val;
    if (val.length > 0) {
      wx.setClipboardData({
        data: val,
        success: function(res) {
          console.log(res);
        }
      });
    }
  },
  contentTap: function() {
    var content = this.data.detail.content;
    content = content.replace(/<.*?>/g, "");
    wx.setClipboardData({
      data: content,
      success: function(res) {
        console.log(res);
      }
    });
  },
})